package com.ray.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.xmlbeans.impl.xb.xsdschema.impl.PublicImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.ray.bean.DatatablesViewPageBean;
import com.ray.bean.InternetThingCardBean;
import com.ray.mapper.InternetThingCardMapper;
import com.ray.util.ReturnStringUtil;
import com.ray.util.StringUtil;

@Service
public class InternetThingCardService {
	
	@Autowired
	InternetThingCardMapper internetThingCardMapper;

	/**
	 * @isUrl true
	 * 添加物联网卡信息
	 * @param map
	 * @return
	 */
	public String addInternetThingCard(Map map) {
		StringUtil.checkIsTrue(map, "internetThingsCard", "未获取到物联网卡id");
		StringUtil.checkIsTrue(map, "status", "未获取到状态");
		Assert.isTrue(this.checkCardIsRepeat(map),"物联网卡id或者二维码编号重复");
		int row=internetThingCardMapper.addInternetThingCard(map);
		Assert.isTrue(row>0,ReturnStringUtil.ADD_ERROR);
		return ReturnStringUtil.ADD_SUCCESS;
	}

	/**
	 * 通过物联网卡id校验重复
	 * @param map
	 * @return
	 */
	public  boolean checkCardIsRepeat(Map map) {
		Map repeatMap=new HashMap();
		repeatMap.put("internetThingsCard", map.get("internetThingsCard"));
		List<InternetThingCardBean> list=this.getInternetThingCardByTowCard(map);
		if(list != null && list.size() >  0) {
			return false ;
		}
		return true ;
	}

	/**
	 * 通过两个编号查找
	 * @param map
	 * @return
	 */
	private List<InternetThingCardBean> getInternetThingCardByTowCard(Map map) {
		return internetThingCardMapper.getInternetThingCardByTowCard(map);
	}

	
	
	/**
	 * 获取物联网卡信息
	 * @param map
	 * @return
	 */                                 
	private List<InternetThingCardBean> getInternetThingCard(Map map) {
		return internetThingCardMapper.getInternetThingCard(map);
	}
	
	
	/**
	 * @isUrl true
	 * 分页获取物联网卡信息
	 * @param map
	 * @return
	 */
	public DatatablesViewPageBean<InternetThingCardBean> getInternetThingCardPage(Map map){
		DatatablesViewPageBean<InternetThingCardBean> datatablesViewPageBean=new DatatablesViewPageBean<InternetThingCardBean>();
	    List<InternetThingCardBean> list=this.getInternetThingCard(map);
	    int  count = internetThingCardMapper.getInternetThingCardCount(map);
	    datatablesViewPageBean.setReturnData(list);
	    datatablesViewPageBean.setiTotalDisplayRecords(count);
	    datatablesViewPageBean.setiTotalRecords(count);
		return datatablesViewPageBean;
	}
	
	
	/**
	 * @isUrl true 
	 * id 
	 * 修改物联网卡
	 * @param map
	 * @return
	 */
	public String updateInternetThingCard(Map map) {
		StringUtil.checkIsTrue(map, "id", "未获取到id");
		int row=internetThingCardMapper.updateInternetThingCard(map);
		Assert.isTrue(row>0,ReturnStringUtil.UPDATE_ERROR);
		return ReturnStringUtil.UPDATE_SUCCESS;
	}
	
	/**
	 * @isUrl true  
	 * 获取本公司的物联网卡
	 * @param map
	 * @return
	 */
	public List<InternetThingCardBean> getCompanyInternetThingCard(Map map){
		return internetThingCardMapper.getCompanyInternetThingCard(map);
	}
	
}
